
export default {
	data() {
		return {
			datagetter_onceFn: [],
			datagetter_realtimeFn: [],
			realtimeInterval: 10000,
		}
	},
	beforeDestroy() {
		clearTimeout(this.datagetter_realtimeTimer);
	},
	methods: {
		registerOnce(fns = []) {
			this.registerFns(fns, this.datagetter_onceFn, this.getOnce);
		},
		registerRealtime(fns = []) {
			this.registerFns(fns, this.datagetter_realtimeFn, this.getRealtime);
		},
		registerFns(fns = [], target, callback) {
			if (!Array.isArray(target)) return;
			fns.forEach(i => {
				if (i instanceof Function && !target.includes(i)) {
					target.push(i);
				}
			})
			if (callback instanceof Function) callback();
		},
		unregisterFns(fn, target) {
			if (!Array.isArray(target) || !fn instanceof Function) return;
			target.splice(target.findIndex(fn), 1);
		},
		getOnce() {
			this.datagetter_onceFn.forEach(fn => fn());
		},
		getRealtime() {
			clearTimeout(this.datagetter_realtimeTimer);
			this.datagetter_realtimeFn.forEach(fn => fn());
			this.datagetter_realtimeTimer = setTimeout(this.getRealtime, this.realtimeInterval);
		}
	}
}